package org.dreamfly.core.bean;

import org.apache.struts2.ServletActionContext;
import org.dreamfly.core.page.PageResultSet;
import org.dreamfly.core.util.SessionContext;
import org.dreamfly.core.util.XMLReader;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-12-4
 * Time: 21:19:42
 * To change this template use File | Settings | File Templates.
 */
public class ResultSetPoolImpl
        implements ResultSetPool,InitializingBean, DisposableBean {

    /**
     * sessionId和searchID之间的分隔符号
     */
    private static final String KEY_SPAR = "_";

    /**
     * 服务中心容器
     */
    private  Map<String, Object> resultSetContainer = null;

    /**
     * 存放服务器IP映射的容器
     */
    private  Map<String, Object> ipMapperContainer = null;
    /**
     * 服务Key列表，内部使用
     */
    private  List<String> resultSetNames = null;

    @Override
    public void createResultSet(String sessionId, String searchId, PageResultSet pageResultSet) {
        String KEY = sessionId + KEY_SPAR + searchId;
        if(null == resultSetNames || null == resultSetContainer){
            try {
                this.afterPropertiesSet();
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        resultSetNames.add(KEY);
        resultSetContainer.put(KEY,pageResultSet);
    }

    @Override
    public PageResultSet getResultSet(String sessionId, String searchId) {
        String KEY = sessionId + KEY_SPAR + searchId;
        return (PageResultSet) resultSetContainer.get(KEY);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setIpInfos(Map map) {
        this.ipMapperContainer = map;
    }

    @Override
    public ServerIpInfo getIpInfo(String serverName) {
        if(null == ipMapperContainer){
            ipMapperContainer = XMLReader.readServerIpInfo(ServletActionContext.getServletContext().getRealPath("") + "/WEB-INF/ip_name.xml");
        }
        return (ServerIpInfo) ipMapperContainer.get(serverName);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void destroyResultSet() {
        SessionContext sessionContext = SessionContext.getInstance();
        for(String key : resultSetNames){
            String sessionId = key.split(KEY_SPAR)[0];
            HttpSession session = sessionContext.getSession(sessionId);
            if(null != session)
                resultSetContainer.remove(key);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.resultSetContainer = new HashMap<String,Object>();
        this.resultSetNames = new ArrayList<String>();
    }

    @Override
    public void destroy() throws Exception {
        this.resultSetContainer = null;
        this.resultSetNames = null;
    }

}
